package cn.tx.service.impl;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.Set;

import cn.tx.business.OrderProduct;
import cn.tx.dao.ConsoleLogDao;
import cn.tx.dao.OrderDetailDao;
import cn.tx.dao.OrderModelDao;
import cn.tx.model.ConsoleLog;
import cn.tx.model.Emp;
import cn.tx.model.OrderDetail;
import cn.tx.model.OrderModel;
import cn.tx.query.OrderModelQuery;
import cn.tx.query.QueryOrderProduct;
import cn.tx.service.OrderModelService;
import cn.tx.util.ERPConstants;

public class OrderModelServiceImpl extends BaseServiceImpl<OrderModel, OrderModelQuery> implements OrderModelService {

	private OrderModelDao orderModelDao;
	
	private ConsoleLogDao consoleLogDao;

	private OrderDetailDao orderDetailDao;
	
	
	

	public void setOrderDetailDao(OrderDetailDao orderDetailDao) {
		this.orderDetailDao = orderDetailDao;
	}

	public void setConsoleLogDao(ConsoleLogDao consoleLogDao) {
		this.consoleLogDao = consoleLogDao;
	}

	public void setOrderModelDao(OrderModelDao orderModelDao) {
		this.orderModelDao = orderModelDao;
		this.baseDao = orderModelDao;
	}

	@Override
	public void auditOrder(Emp emp, Integer orderId, Integer orderState, String note) {
		OrderModel order = orderModelDao.getObjectById(orderId);
		order.setOrderState(orderState);
		order.setOrderChecker(emp);
		order.setCheckTime(new Date());
		if(orderState == 2){
			order.setOrderType(ERPConstants.ORDER_TYPE_TRAN);
		}
		
		ConsoleLog cl = new ConsoleLog();
		cl.setEntityId(orderId);
		cl.setNote(note);
		cl.setOptTime(new Timestamp(new Date().getTime()));
		cl.setOptType("审核");
		cl.setTableName("order_model");
		cl.setEmp(emp);
		
		consoleLogDao.save(cl);
		
	}

	@Override
	public void updateSubmitAuidOrder(OrderModel orderModel,
			Set<OrderDetail> ods) {
		for(OrderDetail od : ods){
			orderDetailDao.delete(od);
		}
		orderModelDao.update(orderModel);
		
		
	}

	@Override
	public List<OrderProduct> staOrderProduct(QueryOrderProduct qop) {
		return orderModelDao.staOrderProduct(qop);
	}
	
	

}
